﻿using UnityEngine;
using Random = System.Random;

namespace GameFramework.skill.effect.impl
{
    //创建子弹效果
    public class BulletSkillEffect:ISkillEffect
    {
        public EffectConfig effectConfig;
        public void excute(Skill skill)
        {
            Debug.Log("创建子弹");
            float[] configValues = effectConfig.values;
            float bulletId = configValues[0];
            float num = configValues[1];
            if (num <= 0)
            {
                Debug.LogWarning("召唤数量小于1，召唤失败！技能id："+skill.skillId);
                return;
            }

            BulletConfig bulletConfig = BulletConfigManager.Instance.getConfig((int)bulletId);
            string prefabName = bulletConfig.prefabName;
            //TODO 缓存
            GameObject bulletPrefab = UnityEngine.Resources.Load<GameObject>(prefabName);
            GameObject owner = skill.owner;
            for (int i = 0; i < num; i++)
            {
                GameObject bullet = GameObject.Instantiate(bulletPrefab, owner.transform.position, owner.transform.rotation);
                BulletCtl bulletCtl = bullet.GetComponent<BulletCtl>();
                //TODO 子弹目标选择策略配置
                int index = 0;
                if (skill.targets.Count > 1)
                {
                    Random random = new Random();
                    index = random.Next(0, (int)num);
                }
                
                bulletCtl.target = skill.targets[index].gameObject;
                bulletCtl._skill = skill;
                bulletCtl.config = bulletConfig;
                bulletCtl.go();
            }

        }
    }
}